<?php require_once 'model/db2.php'; ?>

<h1>Nguyen Thi Phuong Hang</h1>
<form method="POST">
    Min Like : <input type="text" name="Min"><br>
    Max Like:  <input type="text" name="Max"><br>
    Keyword : <input type="text" name="Keyword"><br>
    Company: <select name="id_company">
        <?php
        $companies = getPage("SELECT * FROM companies");
        ?>
        <option value="0"><?php echo "Company" ?></option>
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select> </br>
    <select name="id_app">
        <?php
        $apps = getApps("SELECT * FROM apps");
        ?>
        <option value="0"><?php echo "Apps" ?></option>
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
        <?php endforeach; ?>
    </select></br>

    <input type="submit" name="submit" value="Search">
</form>

<?php
$query = "SELECT * FROM pages";
$pages = getPage($query);
?>
<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['Min'];
    $max_fans = $_POST['Max'];
    $keyword = $_POST['Keyword'];
    $company = $_POST["id_company"];
    $app = $_POST["id_app"];

    if (!empty($min_fans))
        $min_fans = "likes >= $min_fans AND";
    else
        $min_fans = "";
    if (!empty($max_fans))
        $max_fans = "likes <= $max_fans AND";
    else
        $max_fans = "";
    if (!empty($company))
        $company = "companies.id= $company AND";
    else
        $company = "";
    if (!empty($app))
        $app = "apps.id= $app AND";
    else
        $app = "";


    $query = "SELECT * FROM pages
                    INNER JOIN apps_pages ON pages.id=apps_pages.id_page
                    INNER JOIN apps ON apps.id=apps_pages.id_app
                    INNER JOIN companies ON companies.id=apps.id_company
                  WHERE $min_fans
                        


                        $company
                        $app
                        (description LIKE '% $keyword %')
                        LIMIT 0, 10 ";
    var_dump($query);
    $pages = getPage($query);
    ?> 
    <?php if (!empty($pages)): ?>

        <table border ="1">
            <tr>
                <td class="pid">STT </td>
                <td class="picture">picture </td>
                <td class="id">ID</td>
                <td class="name">Name</td>
                <td class="likes">Likes</td>
                <td class="talking">Talking</td>
                <td class="category">Category</td>
                
            </tr>
        <?php foreach ($pages as $page): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><img src = '<?php echo $page["picture"] ?>'></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                   
                </tr>
        <?php endforeach; ?>
        </table>
    <?php endif; ?>
<?php endif; ?>
